Skip to content

fix: bigquery throws on if as first argument - #2959

Open
harryfoster2006 wants to merge 1 commit into
JSQLParser:masterfrom
harryfoster2006:fix/bigquery-if-as-first-argument
Open

harryfoster2006 wants to merge 1 commit into
JSQLParser:masterfrom
harryfoster2006:fix/bigquery-if-as-first-argument

Conversation

@harryfoster2006

Copy link
Copy Markdown
Contributor

In BigQuery,IF as the first element of an array literal inside a function call fails to parse:

CREATE OR REPLACE TABLE `p.d.t` AS
SELECT ARRAY(SELECT w FROM UNNEST([IF(a > 1, 'x', NULL)]) w) AS flags
FROM `p.d.src`

Encountered: "[" / "[", at line 2, column 35
Was expecting one of: <K_RESPECT>, <K_HAVING>, <K_IGNORE>, <K_LIMIT>, <CLOSING_BRACKET> ...

The same statement parses when IF isn't the first element, e.g. UNNEST(['a', IF(...)]).

Cause

IF is a reserved keyword, so RelObjectName only accepts it as a function name when isReservedKeywordAsIdentifier() allows it. That guard checks the previous token, and [ was not in its list of delimiters that start an expression position (., ,, :, (, =, operators). [ fell through to isReservedKeywordSafeByFollower(), which rejects IF when followed by (.

InternalFunction uses a syntactic LOOKAHEAD(2) to decide whether to enter its argument list. That probe runs the guard, so it failed, the whole optional argument block was skipped, and the parser then hit [ where it expected ). With , before IF the guard already returned true, which is why only the first-element case failed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant